package com.redhat.redwork.widget.dropdown;

import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

/**
 * load data for dropdown
 * 
 * @author 叶志明
 * 
 */
@SuppressWarnings("serial")
public class DropdownAction extends ActionSupport {

	/** data for dropdown */
	protected Map<String, String> dropdownData;
	
	@Autowired
	protected DropdownService dropdownService;

	/** data model name define in component */
	protected String dataModel;

	/** the selected value of master dropdown */
	protected String parentValue;
	
	@RequiredFieldValidator(type = ValidatorType.FIELD, message = "dataModel must be defined")
	public void setDataModel(String dataModel) {
		this.dataModel = dataModel;
	}

	public void setParentValue(String parentValue) {
		this.parentValue = parentValue;
	}

	/**
	 * retrieve data for drop down construction
	 * 
	 * @return
	 */
	public Map<String, String> getDropdownData() {
		return dropdownData;
	}

	/**
	 * service inject
	 * 
	 * @param dropdownService
	 */
	public void setDropdownService(DropdownService dropdownService) {
		this.dropdownService = dropdownService;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		Map params = ServletActionContext.getRequest().getParameterMap();
		dropdownData = dropdownService.loadDropdownData(dataModel, parentValue,
				params);
		return SUCCESS;
	}

	public DropdownService getDropdownService() {
		return dropdownService;
	}

	public String getDataModel() {
		return dataModel;
	}

	public String getParentValue() {
		return parentValue;
	}

	public void setDropdownData(Map<String, String> dropdownData) {
		this.dropdownData = dropdownData;
	}
	
	
	
}
